package com.mk.admin.service.impl;

import com.mk.admin.mapper.*;
import com.mk.admin.model.dto.*;
import com.mk.admin.model.entity.*;
import com.mk.admin.model.vo.*;
import com.mk.admin.service.*;
import com.mk.admin.util.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.stereotype.*;

import java.util.*;

@Service
public class ChinaDrugDiServiceImpl implements ChinaDrugDiService {
    @Autowired
    private ChinaDrugDiMapper chinaDrugDiMapper;


    @Override
    public ChinaDrugDiDto select(MKCheckVo vo) {
        ChinaDrugDiEntity entity = JSONCopy.copy(vo, ChinaDrugDiEntity.class);
        ChinaDrugDiDto dto = new ChinaDrugDiDto();

        HashSet<String> codes =new HashSet<>();
        vo.getOrder().forEach(item->{
            codes.add(item.getCode());
        });

        //两两配对查询后台
        List<List> pairs = new ArrayList<>();
        String[] elements = codes.toArray(new String[0]);
        for (int i = 0; i < elements.length; i++) {
            for (int j = i + 1; j < elements.length; j++) {
                List<String> pair = new ArrayList<>();
                pair.add( elements[i]);
                pair.add( elements[j]);
                pairs.add(pair);
            }
        }

        for (int i = 0; i < pairs.size() ; i++) {
            String codeA = (String) pairs.get(i).get(0);
            String codeB = (String) pairs.get(i).get(1);
            entity.setEnable(1);
            entity.setCodeA(codeA);
            entity.setCodeB(codeB);
            List<ChinaDrugDiDto> list = chinaDrugDiMapper.select(entity);
            if (list != null && list.size() > 0) {
                dto.setDrugA(list.get(0).getDrugA());
                dto.setCite(list.get(0).getCite());
                dto.setLevel(list.get(0).getLevel());
                dto.setWay(list.get(0).getWay());
                dto.setDescribe(list.get(0).getDescribe());
            }
        }
        return dto;
    }
}
